# day5数据筛选与可视化.py
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def scatter_filter_demo(seed=42):
    """
    DataFrame筛选与散点图可视化案例
    """
    np.random.seed(seed)  # 保证结果可复现
    # 创建数据
    data = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [5, 4, 3, 2, 1]})

    # 随机生成筛选阈值
    threshold = np.random.randint(1, 5)
    print("筛选阈值:", threshold)

    # 根据阈值筛选数据
    filtered_data = data[data['A'] > threshold]
    print("筛选后的数据:\n", filtered_data)
    
    # 绘制散点图
    plt.figure(figsize=(6, 4))
    plt.scatter(filtered_data['A'], filtered_data['B'], color='blue', marker='o', s=100, label='Filtered Points')
    plt.title(f'Scatter Plot (Filter: A > {threshold})')
    plt.xlabel('A')
    plt.ylabel('B')
    plt.legend()
    plt.grid(True, alpha=0.3)
    plt.tight_layout()
    # plt.savefig('filtered_scatter.png')
    plt.show()

if __name__ == "__main__":
    scatter_filter_demo()
